/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: win_atomic.c
 *
 * Purpose: implementation atomic operation with win atomic API
 *
 * Developer:
 *   wen.gu , 2022-01-21
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#if defined(_MSC_VER)
#include "cbus/core/cbus_atomic.h"


#include "cbus/core/cbus_log.h"


/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/


/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
/**
*
*@brief  add a value to orgValue as atomic operation
*
*@param  orgValue [io] origin value.
*@param  addValue [in] add value.
*
*@return previous *orgValue.
*
*@see
*/
cbus_atomic_int cbus_atomic_exchange_add(cbus_atomic_int *orgValue, cbus_atomic_int addValue)
{
    return InterlockedExchangeAdd((_Inout_ LONG volatile*)orgValue, addValue);
}

/**
*
*@brief  add 1 to orgValue as atomic operation
*
*@param  orgValue [io] origin value.
*
*@return  previous *orgValue.
*
*@see
*/
cbus_atomic_int cbus_atomic_increment(cbus_atomic_int *orgValue)
{
    return InterlockedExchangeAdd((_Inout_ LONG volatile*)orgValue, 1);
}

/**
*
*@brief  dec  1 to orgValue as atomic operation
*
*@param  orgValue [io] origin value.
*
*@return previous *orgValue.
*
*@see
*/
cbus_atomic_int cbus_atomic_decrement(cbus_atomic_int *orgValue)
{
    return InterlockedExchangeAdd((_Inout_ LONG volatile*)orgValue, -1);
}

/**
*
*@brief  atomic 'or' operation
*
*@param  orgValue [io] origin value.
*
*@return previous orgValue.
*
*@see
*/
cbus_atomic_int cbus_atomic_or(cbus_atomic_int *orgValue, cbus_atomic_int value)
{
    return InterlockedOr((_Inout_ LONG volatile*)orgValue, value);
}

/**
*
*@brief  atomic 'or' operation
*
*@param  orgValue [io] origin value.
*
*@return previous orgValue.
*
*@see
*/
cbus_atomic_int cbus_atomic_and(cbus_atomic_int *orgValue, cbus_atomic_int value)
{
    return _InterlockedAnd((_Inout_ LONG volatile*)orgValue, value);
}

/**
 *
 *@brief  compare orgValue with old_value, if equal then orgValue with new_value
 *
 *@param  orgValue  [io] origin value.
 *        old_value [in] old value.
 *        new_value [in] new value.
 *
 *@return This returns GTRUE if the new value was successfully stored, 
 *        which will only happen when *org_value == old_value.
 *
 *@see
 */
GBOL cbus_atomic_compare_exchange(cbus_atomic_int *orgValue, cbus_atomic_int old_value, cbus_atomic_int new_value)
{
    return (old_value == InterlockedCompareExchangeRelease((_Inout_ LONG volatile*)orgValue, new_value, old_value)) ? GTRUE : GFALSE;
}

//////////////////////////////////////////////////////////////////////////

/**
*
*@brief  add a value to orgValue as atomic operation(orgValue is 64 bit)
*
*@param  orgValue [io] origin value.
*@param  addValue [in] add value.
*
*@return previous *orgValue.
*
*@see
*/
cbus_atomic_int64 cbus_atomic_exchange_add64(cbus_atomic_int64 *orgValue, cbus_atomic_int64 addValue)
{
    return InterlockedExchangeAdd64((_Inout_ LONGLONG volatile*)orgValue, addValue);
}

/**
*
*@brief  add 1 to orgValue as atomic operation(orgValue is 64 bit)
*
*@param  orgValue [io] origin value.
*
*@return previous *orgValue.
*
*@see
*/
cbus_atomic_int64 cbus_atomic_increment64(cbus_atomic_int64 *orgValue)
{
   // return Interlockedincrement64(orgValue);
    return InterlockedExchangeAdd64((_Inout_ LONGLONG volatile*)orgValue, 1);
}

/**
*
*@brief  dec  1 to orgValue as atomic operation(orgValue is 64 bit)
*
*@param  orgValue [io] origin value.
*
*@return previous *orgValue.
*
*@see
*/
cbus_atomic_int64 cbus_atomic_decrement64(cbus_atomic_int64 *orgValue)
{
    //return Interlockeddecrement64(orgValue);
    return InterlockedExchangeAdd64((_Inout_ LONGLONG volatile*)orgValue, -1);
}

#endif /** end of _MSC_VER */